package cn.edu.tju.elm.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.core.security.service.UserService;
import cn.edu.tju.elm.model.Cart;
import cn.edu.tju.elm.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@Tag(name = "管理购物车", description = "对购物车内的商品增删改查")
public class CartController {
    @Autowired
    private UserService userService;

    @Autowired
    private CartService cartService;

    @PostMapping("/carts")
    @Operation(summary = "添加购物车项", description = "添加商品到购物车")
    public HttpResult<Cart> addCartItem(@RequestBody Cart cart){
        try {
            Cart savedCart = cartService.addCartItem(cart);
            return HttpResult.success(savedCart);
        } catch (IllegalArgumentException e) {
            return HttpResult.failure(ResultCodeEnum.BAD_REQUEST, e.getMessage());
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "添加购物车项失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/carts")
    @Operation(summary = "获取用户购物车", description = "获取指定用户的购物车列表")
    public HttpResult<List<Cart>> getUserCart(@RequestParam Long userId){
        List<Cart> carts = cartService.getCartItemsByUserId(userId);
        return HttpResult.success(carts);
    }

    @PutMapping("/carts/{id}")
    @Operation(summary = "更新购物车项数量", description = "更新购物车中商品的数量")
    public HttpResult<Cart> updateCartItemQuantity(
            @PathVariable Long id,
            @RequestBody CartQuantityUpdateRequest request) {

        Cart updatedCart = cartService.updateCartItemQuantity(id, request.getQuantity());
        if(updatedCart != null) {
            return HttpResult.success(updatedCart);
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }

    /**
     * 内部静态类，用于接收更新数量的请求
     */
    public static class CartQuantityUpdateRequest {
        private Integer quantity;

        public Integer getQuantity() {
            return quantity;
        }

        public void setQuantity(Integer quantity) {
            this.quantity = quantity;
        }
    }
    
    @DeleteMapping("/carts/{id}")
    @Operation(summary = "删除购物车项", description = "从购物车中删除指定商品")
    public HttpResult<Void> deleteCartItem(@PathVariable Long id){
        boolean deleted = cartService.deleteCartItem(id);
        if(deleted) {
            return HttpResult.success();
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }
    
    @DeleteMapping("/carts")
    @Operation(summary = "清空用户购物车", description = "清空指定用户的购物车")
    public HttpResult<Void> clearUserCart(@RequestParam Long userId){
        boolean cleared = cartService.clearUserCart(userId);
        if(cleared) {
            return HttpResult.success();
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }
}
